package com.zztpc.easybuy.util;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MyUtil {
    //读取配置文件
    public static Map<String,String> readXML()throws Exception{
        Map<String,String> map=new HashMap<String, String>();
        SAXBuilder saxBuilder=new SAXBuilder();
        String path=MyUtil.class.getResource("/").getPath()+"db-config.xml";
        Document doc=saxBuilder.build(path);
        //得到根节点
        Element rootElment=doc.getRootElement();
        //System.out.println("根节点:"+rootElment.getName());
        //得到子节点
        List<Element> listall=rootElment.getChildren();
        //循环
        /*
        for(int i=0;i<listall.size();i++){
            System.out.println("子节点"+i+":"+listall.get(i).getName());
        }
        */
        //迭代
        Iterator iter=listall.iterator();
        while (iter.hasNext()){//迭代器有几个方法     判断有没有元素hasNext()     有元素next()
            Element e=(Element) iter.next();
            //System.out.println(e.getName()+":***************"+e.getText());
            //放入map
            map.put(e.getName(),e.getText());
        }
        return map;
    }

    public static void main(String[] args) {
        try {
            System.out.println(MyUtil.readXML().size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
